<?php

namespace app\admin\controller\Order;

use app\common\controller\Backend;

/**
 * 回收站管理
 */
class Recyclebin extends Backend
{
    // 关联查询
    protected $relationSearch = true;
    // 快速查询，搜索指定的哪些字段
    protected $searchFields = '';
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Order.Order');
    }

    // 查看
    public function index(){
        //设置过滤方法  过滤空格和标签信息
        $this->request->filter(['trim', 'strip_tags']);

        if ($this->request->isAjax()) {
            //主键
            if ($this->request->request("keyField")) {
                // 自定义搜索查询
                return $this->selectpage();
            }

            // 生成查询所需要的条件,排序方式
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->with(['airline','startPoint','endPoint','type','timelist','unit','uav'])
                ->where($where)
                ->where('order.is_del',1)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->with(['airline','startPoint','endPoint','type','timelist','unit','uav'])
                ->where($where)
                ->where('order.is_del',1)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

        return $this->view->fetch();
    }

    // 销毁
    public function destroy($ids = null){
        $ids=$ids ? $ids:$this->request->param('ids','','trim');

        $list=$this->model->select($ids);
        if(empty($list)){
          $this->error('没有该订单');
        }else{
          $result = $this->model->where(['id'=>['IN',$ids]])->delete();
          if($result===false){
            $this->error($this->model->getError());
            exit;
          }else{
            $this->success('销毁成功');
            exit;
          }
        }
    }


    // 还原
    public function restore($ids = null){
        $ids=$ids ? $ids:$this->request->param('ids','','trim');

        $list=$this->model->select($ids);
        if(empty($list)){
            $this->error('没有该订单');
          }else{
            $result=$this->model->where(['id'=>['IN',$ids]])->update(['is_del'=>2]);
            if($result===false){
              $this->error($this->model->getError());
              exit;
            }else{
              $this->success('恢复成功');
              exit;
            }
          }
    }
}
